<!DOCTYPE html>
<html>
<head>
    <title>文件处理结果</title>
    <style>
        /* 确认对话框样式 - 淡蓝色主题 */
        .confirm-dialog {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #e3f2fd;
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
            border: 1px solid #90caf9;
            width: 320px;
            font-family: Arial, sans-serif;
        }

        .confirm-dialog p {
            margin: 0 0 20px 0;
            color: #1565c0;
            font-size: 16px;
            line-height: 1.5;
        }

        .dialog-buttons {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        .dialog-buttons button {
            padding: 8px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.2s ease;
            font-size: 14px;
        }

        #cancelBtn {
            background-color: #e0e0e0;
            color: #616161;
        }

        #cancelBtn:hover {
            background-color: #bdbdbd;
        }

        #confirmBtn {
            background-color: #42a5f5;
            color: white;
        }

        #confirmBtn:hover {
            background-color: #1e88e5;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        /* 基础样式调整 */
        body {
            margin: 0;
            padding: 0;
            height: calc(100vh - 20px);
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
            overflow: hidden;
        }

        .analysis-results-container {
            font-size: 14px;
            line-height: 1.4;
            height: 100%;
            padding: 8px;
            padding-bottom: 28px;
        }

        .results-title {
            font-size: 15px;
            font-weight: 600;
            padding: 6px 8px;
            background-color: #f5f5f5;
            border-radius: 3px;
            margin-bottom: 8px;
            display: inline-block;
        }

        #results-container {
            overflow-y: auto;
            height: calc(100% - 30px);
        }

        .file-container {
            margin-bottom: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 8px;
            background-color: #fff;
            box-shadow: 0 1px 2px rgba(0,0,0,0.05);
            transition: background-color 0.3s ease;
        }

        .file-container:hover {
            background-color: #e3f2fd;
        }

        .file-container:hover .delete-btn:hover ~ .file-title,
        .file-container:hover .delete-btn:hover {
            background-color: #ffebee;
        }

        .file-container:hover .delete-btn:hover ~ .file-results,
        .file-container:hover .delete-btn:hover ~ .file-title + .file-results {
            background-color: #ffebee;
        }

        .file-title {
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            padding: 6px 8px;
            background-color: #f5f5f5;
            border-radius: 3px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: background-color 0.3s ease;
        }

        .delete-btn {
            color: #999;
            margin-left: 10px;
            padding: 0 5px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .delete-btn:hover {
            color: #f44336;
            transform: scale(1.2);
        }

        .file-results {
            margin-top: 8px;
            padding: 8px;
            background-color: #f9f9f9;
            border-radius: 3px;
            font-size: 13px;
            display: block !important; /* 强制显示结果区域 */
        }

        .result-item {
            display: block;
            margin-bottom: 12px;
        }

        .result-title {
            font-weight: bold;
            margin-bottom: 4px;
            display: block;
        }

        .result-value {
            color: #6c757d;
            word-break: break-word;
            margin-top: 4px;
            white-space: pre-line;
            word-break: break-word;
            display: block;
        }

        .total-workload {
            font-weight: bold;
            background-color: #e3f2fd;
            padding: 8px;
            border-radius: 4px;
            margin-bottom: 12px;
        }

        .total-workload .result-title {
            color: #1565c0;
        }

        .total-workload .result-value {
            color: #0d47a1;
            font-size: 1.1em;
        }
    </style>
</head>
<body>
<div class="analysis-results-container">
    <div class="results-title">项目分析结果</div>
    <div id="results-container">
        <!-- 文件列表将在这里动态添加 -->
    </div>
</div>

<!-- 删除确认对话框 -->
<div id="confirmDialog" class="confirm-dialog">
    <p>确定要删除这个文件及其所有分析结果吗？</p>
    <div class="dialog-buttons">
        <button id="cancelBtn">取消</button>
        <button id="confirmBtn">确定删除</button>
    </div>
</div>

<script>
    let currentFileToDelete = null;

    function showConfirmDialog(fileElement) {
        currentFileToDelete = fileElement;
        document.getElementById('confirmDialog').style.display = 'block';
    }

    function hideConfirmDialog() {
        document.getElementById('confirmDialog').style.display = 'none';
    }

    function confirmDelete() {
        if (currentFileToDelete) {
            currentFileToDelete.remove();
            hideConfirmDialog();
        }
    }

    document.getElementById('confirmBtn').addEventListener('click', confirmDelete);
    document.getElementById('cancelBtn').addEventListener('click', hideConfirmDialog);

    const wsScheme = window.location.protocol === "https:" ? "wss://" : "ws://";
    const wsPath = wsScheme + window.location.host + "/ws/results/?page=result";
    const socket = new WebSocket(wsPath);

    socket.onopen = function(e) {
        console.log("WebSocket连接已建立");
    };

    socket.onmessage = function(e) {
        console.log("原始消息:", e.data);
        try {
            const data = JSON.parse(e.data);
            console.log("解析后的数据:", data);

            const container = document.getElementById('results-container');

            if (data.type === 'analysis_results') {
                // 查找或创建文件容器
                let targetContainer = Array.from(container.querySelectorAll('.file-container')).find(
                    el => el.querySelector('.file-title span').textContent.includes(data.file_name)
                );

                if (!targetContainer) {
                    const newFile = document.createElement('div');
                    newFile.className = 'file-container';
                    newFile.innerHTML = `
                        <div class="file-title" onclick="toggleFileResults(this)" data-expanded="true">
                            <span>▼ ${data.file_name}</span>
                            <span class="delete-btn" onclick="event.stopPropagation(); showConfirmDialog(this.parentElement.parentElement)">×</span>
                        </div>
                        <div class="file-results" style="display: block"></div>
                    `;
                    container.appendChild(newFile);
                    targetContainer = newFile;
                }

                // 填充结果内容
                const resultsContainer = targetContainer.querySelector('.file-results');
                let html = '';

                // 总工作量显示
                html += `
                    <div class="result-item total-workload">
                        <span class="result-title">总工作量:</span>
                        <span class="result-value">${data.total_workload || '未计算'}</span>
                    </div>
                `;

                // 各个结果项
                data.results.forEach(result => {
                    html += `
                        <div class="result-item">
                            <span class="result-title">${result.id}. ${result.title}:</span>
                            <span class="result-value">${result.content}</span>
                        </div>
                    `;
                });

                resultsContainer.innerHTML = html;

                // 确保可见状态
                resultsContainer.style.display = 'block';
                targetContainer.querySelector('.file-title').setAttribute('data-expanded', 'true');
                targetContainer.querySelector('.file-title span').innerHTML = `▼ ${data.file_name}`;

                // 滚动到底部
                container.scrollTop = container.scrollHeight;
                // 向父页面发送消息
                window.parent.postMessage({
                    type: 'training_complete',
                    status: 'done',
                }, '*');
            }
            else if (data.type === 'error') {
                alert(`错误: ${data.message}`);
            }
        } catch (err) {
            console.error("消息解析失败:", err);
        }
    };

    function toggleFileResults(element) {
        const isExpanded = element.getAttribute('data-expanded') === 'true';
        const resultsContainer = element.nextElementSibling;
        const arrowSpan = element.querySelector('span:first-child');

        resultsContainer.style.display = isExpanded ? 'none' : 'block';
        element.setAttribute('data-expanded', !isExpanded);
        arrowSpan.innerHTML = isExpanded ? '▼ ' : '▲ ';
    }
</script>
</body>
</html>